package com.xjiye_test.payServer.servletGroup.weixin;

import com.xiaoleilu.hutool.db.Entity;
import com.xiaoleilu.hutool.db.Page;
import com.xiaoleilu.hutool.db.PageResult;
import com.xjiye_test.payServer.dao.OrderDao;
import com.xjiye_test.payServer.pojo.PayRecord;
import com.xjiye_test.payServer.serverletUtil.IdWorker;
import com.xjiye_test.payServer.serverletUtil.ReadBytes;
import com.xjiye_test.payServer.serverletUtil.Signature;
import com.xjiye_test.payServer.serverletUtil.XMLParser;
import org.xml.sax.SAXException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/19.
 */
public class WeixinOrderQuery extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("content-type","text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String reStr=null;
        try {
            req.setCharacterEncoding("UTF-8");
            int size = req.getContentLength();
            InputStream is = req.getInputStream();
            Map mapRequest = XMLParser.getMapFromXML(new String(ReadBytes.readBytes(is, size)));
            //查询订单是否存在
            OrderDao orderdao=new OrderDao();
            List<String> keys= new ArrayList<>();
            List<String> values= new ArrayList<>();
            keys.add("suppOrderNo=");
            values.add(mapRequest.get("out_trade_no").toString());
            keys.add("suppId=");
            values.add(mapRequest.get("mch_id").toString());
            keys.add("appid=");
            values.add(mapRequest.get("appid").toString());
            PageResult<Entity> order=orderdao.getDataByCondition(new Page(0,10),keys,values);
            if(order.size()==0){
                reStr=Signature.getSignFromResponseString("<xml><return_code><![CDATA[SUCCESS]]></return_code>\n" +
                        "<return_msg><![CDATA[OK]]></return_msg>\n" +
                        "<appid><![CDATA["+mapRequest.get("appid")+"]]></appid>\n" +
                        "<mch_id><![CDATA["+mapRequest.get("mch_id")+"]]></mch_id>\n" +
                        "<nonce_str><![CDATA[XM3mXjlbMQfYxxok]]></nonce_str>\n" +
                        "<sign><![CDATA[2097C52EE3C02EB5DEF5840F08FD7BEB]]></sign>\n" +
                        "<result_code><![CDATA[SUCCESS]]></result_code>\n" +
                        "<out_trade_no><![CDATA["+mapRequest.get("out_trade_no")+"]]></out_trade_no>\n" +
                        "<trade_state><![CDATA[NOTPAY]]></trade_state>\n" +
                        "<trade_state_desc><![CDATA[订单未支付]]></trade_state_desc>\n" +
                        "</xml>","123456");
            }
            if(order.size()==1){
                reStr = Signature.getSignFromResponseString("<xml>\n" +
                        "   <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                        "   <return_msg><![CDATA[OK]]></return_msg>\n" +
                        "   <appid><![CDATA["+mapRequest.get("appid")+"]]></appid>\n" +
                        "   <openid><![CDATA["+ order.get(0).getStr("openid")+"]]></openid>\n" +
                        "   <mch_id><![CDATA["+mapRequest.get("mch_id")+"]]></mch_id>\n" +
                        "   <device_info><![CDATA[1000]]></device_info>\n" +
                        "   <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>\n" +
                        "   <result_code><![CDATA[SUCCESS]]></result_code>\n" +
                        "   <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
                        "   <trade_type><![CDATA[MICROPAY]]></trade_type>\n" +
                        "   <bank_type><![CDATA[CCB_DEBIT]]></bank_type>\n" +
                        "   <total_fee>"+order.get(0).getInt("amount")+"</total_fee>\n" +
                        "   <fee_type><![CDATA[CNY]]></fee_type>\n" +
                        "   <transaction_id><![CDATA["+order.get(0).getStr("wechatOrderNo")+"]]></transaction_id>\n" +
                        "   <out_trade_no><![CDATA["+mapRequest.get("out_trade_no")+"]]></out_trade_no>\n" +
                        "   <attach><![CDATA[ok]]></attach>\n" +
                        "   <time_end><![CDATA[20141111170043]]></time_end>\n" +
                        "   <trade_state><![CDATA[SUCCESS]]></trade_state>\n" +
                        "</xml>", "123456");
            }
            if(order.size()>1){
                reStr = Signature.getSignFromResponseString("<xml><return_code><![CDATA[SUCCESS]]></return_code>\n" +
                        "<return_msg><![CDATA[OK]]></return_msg>\n" +
                        "<appid><![CDATA["+mapRequest.get("appid")+"]]></appid>\n" +
                        "<mch_id><![CDATA["+mapRequest.get("mch_id")+"]]></mch_id>\n" +
                        "<nonce_str><![CDATA[31SYTLZsYmxYvbTE]]></nonce_str>\n" +
                        "<result_code><![CDATA[SUCCESS]]></result_code>\n" +
                        "<openid><![CDATA["+order.get(0).getStr("openid")+"]]></openid>\n" +
                        "<is_subscribe><![CDATA[N]]></is_subscribe>\n" +
                        "<trade_type><![CDATA["+order.get(0).getStr("paytype")+"]]></trade_type>\n" +
                        "<bank_type><![CDATA[CFT]]></bank_type>\n" +
                        "<total_fee>"+order.get(0).getStr("amount")+"</total_fee>\n" +
                        "<fee_type><![CDATA[CNY]]></fee_type>\n" +
                        "<transaction_id><![CDATA["+order.get(0).getStr("wechatOrderNo")+"]]></transaction_id>\n" +
                        "<out_trade_no><![CDATA["+order.get(0).getStr("suppOrderNo")+"]]></out_trade_no>\n" +
                        "<attach><![CDATA[]]></attach>\n" +
                        "<time_end><![CDATA[20170815143200]]></time_end>\n" +
                        "<trade_state><![CDATA[REFUND]]></trade_state>\n" +
                        "<cash_fee>2</cash_fee>\n" +
                        "<trade_state_desc><![CDATA[订单发生过退款，退款详情请查询退款单]]></trade_state_desc>\n" +
                        "</xml>", "123456");
            }
        } catch (IOException e) {
            e.printStackTrace();
            reStr="<xml><return_code><![CDATA[FAIL]]></return_code>\n" +
                    "<return_msg><![CDATA[XML解析错误]]></return_msg>\n" +
                    "</xml>";
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            reStr="<xml><return_code><![CDATA[FAIL]]></return_code>\n" +
                    "<return_msg><![CDATA[系统错误]]></return_msg>\n" +
                    "</xml>";
        } catch (SAXException e) {
            e.printStackTrace();
            reStr="<xml><return_code><![CDATA[FAIL]]></return_code>\n" +
                    "<return_msg><![CDATA[编码错误]]></return_msg>\n" +
                    "</xml>";
        }catch (SQLException e) {
            e.printStackTrace();
            reStr="<xml><return_code><![CDATA[FAIL]]></return_code>\n" +
                    "<return_msg><![CDATA[系统错误]]></return_msg>\n" +
                    "</xml>";
        } finally {
            out.println(reStr);
            out.flush();
        }
    }
}
